package com.jsnkj.basic.activity

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import androidx.annotation.Nullable
import com.jsnkj.basic.R
import com.jsnkj.basic.bean.MyInformationBean
import com.jsnkj.basic.config.PuzzleConfig
import com.jsnkj.basic.config.PuzzleConfig.START_PHOTO
import com.jsnkj.basic.databinding.ActivityMyInformationBinding
import com.jsnkj.basic.mvvm.BaseMVVMActivity
import com.jsnkj.basic.mvvm.HttpViewModel
import com.jsnkj.basic.photo.PuzzlePhotoActivity
import com.jsnkj.basic.util.SharePreferencesUtil
import com.bumptech.glide.Glide
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions

//我的资料
class MyInformationActivity : BaseMVVMActivity<ActivityMyInformationBinding, HttpViewModel>(),
    View.OnClickListener {
    override val layoutId: Int
        get() = R.layout.activity_my_information


    private var goPhotoType = -1

    lateinit var informationBean: MyInformationBean

    override fun initView(savedInstanceState: Bundle?) {
        mVDB.clickListener = this

        informationBean = SharePreferencesUtil.getInstance().getMyInformationBean()

        com.jsnkj.basic.util.LogUtils.e("我的资料数据：$informationBean")




        if (informationBean.imageAvatar.isNullOrEmpty()) {
            val avatar = SharePreferencesUtil.getInstance().getUserBean()?.user?.avatar
            if (avatar.isNullOrEmpty()) {
                Glide.with(this@MyInformationActivity).load(R.drawable.icon_user)
                    .into(mVDB.imgAvatar)
            } else {
                informationBean.imageAvatar = avatar
                Glide.with(this@MyInformationActivity).load(avatar).into(mVDB.imgAvatar)
            }
        } else {
            Glide.with(this).load(informationBean.imageAvatar).into(mVDB.imgAvatar)
        }

        if (!informationBean.nickname.isNullOrEmpty()) {
            mVDB.edNickname.setText(informationBean.nickname.toString())
        }
        if (!informationBean.name.isNullOrEmpty()) {
            mVDB.edName.setText(informationBean.name.toString())
        }

        if (!informationBean.phone.isNullOrEmpty()) {
            mVDB.edPhone.setText(informationBean.phone.toString())
        }
        if (!informationBean.qq.isNullOrEmpty()) {
            mVDB.edQq.setText(informationBean.qq.toString())
        }

        if (!informationBean.wx.isNullOrEmpty()) {
            mVDB.edWx.setText(informationBean.wx.toString())
        }

        if (!informationBean.imageQR.isNullOrEmpty()) {
            Glide.with(this).load(informationBean.imageQR).into(mVDB.imgQr)
        }

    }


    override fun onClick(view: View) {
        when (view.id) {
            R.id.image_return -> {
                onBackPressed()
            }

            R.id.txt_top_save -> {
                //保存
                saveMyInformation()
            }

            R.id.img_avatar -> {
                //更换头像
                goPhotoType = 0
                goToPuzzlePhoto()
            }

            R.id.txt_update_img -> {
                //修改群二维码
                goPhotoType = 1
                goToPuzzlePhoto()
            }
        }
    }

    private fun saveMyInformation() {
        informationBean.nickname = mVDB.edNickname.text.toString()
        informationBean.name = mVDB.edName.text.toString()
        informationBean.phone = mVDB.edPhone.text.toString()
        informationBean.qq = mVDB.edQq.text.toString()
        informationBean.wx = mVDB.edWx.text.toString()

        com.jsnkj.basic.util.LogUtils.e("我的资料 保存：$informationBean")
        SharePreferencesUtil.getInstance().putMyInformationBean(informationBean)
        onBackPressed()
    }

    fun goToPuzzlePhoto() {
        XXPermissions.with(this) // 申请单个权限
            .permission(Permission.Group.STORAGE)// 申请多个权限
            .request(object : OnPermissionCallback {
                override fun onGranted(permissions: List<String>, all: Boolean) {
                    if (all) {
                        PuzzlePhotoActivity.startActivity(
                            this@MyInformationActivity, 1, START_PHOTO
                        )
                    } else {
                        com.jsnkj.basic.util.ToastUtils.show("获取部分权限成功，但部分权限未正常授予")
                    }
                }

                override fun onDenied(permissions: List<String>, never: Boolean) {
                    if (never) {
                        com.jsnkj.basic.util.ToastUtils.show("被永久拒绝授权，请手动授予存储权限")
                        // 如果是被永久拒绝就跳转到应用权限系统设置页面
                        XXPermissions.startPermissionActivity(
                            this@MyInformationActivity,
                            permissions
                        )
                    } else {
                        com.jsnkj.basic.util.ToastUtils.show("获取存储权限失败")
                    }
                }
            })

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, @Nullable data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == START_PHOTO) {

            if (data != null) {
                // 返回选择的图片地址list
                val arrMediaListPath = data.getStringArrayListExtra(PuzzleConfig.ALBUM_RESULT)

                if (null != arrMediaListPath && arrMediaListPath.size > 0) {
                    if (!TextUtils.isEmpty(arrMediaListPath[0])) {
                        onPhotoResult(arrMediaListPath[0])
                    }
                } else {
                    Toast.makeText(this, "图片获取失败", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    /**
     * 相册返回数据，跳转功能界面
     */
    private fun onPhotoResult(photoPath: String) {
        com.jsnkj.basic.util.LogUtils.e("相册返回数据 goPhotoType == $goPhotoType  photoPath == $photoPath  ")
        if (0 == goPhotoType) {
            informationBean.imageAvatar = photoPath
            Glide.with(this).load(informationBean.imageAvatar).into(mVDB.imgAvatar)
        } else {
            informationBean.imageQR = photoPath
            Glide.with(this).load(informationBean.imageQR).into(mVDB.imgQr)
        }

    }


}